package com.idealighter.game.server.net.http;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;

@Singleton
public class GameNettyHttpServerChannelInitializer extends ChannelInitializer<SocketChannel> {
  private final WebHttpServerHandler handler;

  @Inject
  public GameNettyHttpServerChannelInitializer(WebHttpServerHandler handler) {
    this.handler = handler;
  }

  @Override
  protected void initChannel(SocketChannel ch) throws Exception {
    ch.pipeline().addLast("encoder", new HttpResponseEncoder());
    ch.pipeline().addLast("decoder", new HttpRequestDecoder());

    // 将MessageWapper转HttpResponse,似的HttpResponseEncoder能够解析
    ch.pipeline().addLast(new MessageWapperHttpResponseEncoder());
    ch.pipeline().addLast(new HttpObjectAggregator(65536));
    ch.pipeline().addLast(handler); // 业务处理
  }

  public void shutdown() {
    handler.shutdown();
  }
}
